package com.example.pei.ithinklearnt.present;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.bean.UserSkill;
import com.example.pei.ithinklearnt.model.SkillModel;
import com.example.pei.ithinklearnt.model.SkillModelImp;
import com.example.pei.ithinklearnt.utils.ToastUtil;
import com.example.pei.ithinklearnt.view.interfaces.SkillInterface;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * Created by Pei on 2018/3/9.
 */

public class SkillPresentImp implements SkillPresent {

    private SkillInterface skillInterface;
    private SkillModel skillModel;
   // private DaoOrder ordersDao;


    private static final int MATCH_WANT=5;  //当前用户想学的和查询用户会的相匹配的权值，一个技能
    private static final int MATCH_CAN=3;   //当前用户会的和查询用户想学的技能想匹配的权值，稍微低一点  ，一个技能

    public SkillPresentImp(SkillInterface skillInterface){
        this.skillInterface=skillInterface;
        skillModel=new SkillModelImp();
      //  ordersDao = new DaoOrder(skillInterface.getThisActivity());
    }


    @Override
    public void matchFriend(final matchFinishedListener listener) {
        skillModel.achieveAllUserSkills(new SkillModelImp.achieveAllUserSkillsListerner() {
            @Override
            public void succeed(List<UserSkill> list) {
                matchFreinds(list,listener);
            }

            @Override
            public void failed() {
                ToastUtil.ToastShow(skillInterface.getThisActivity(),"请检查网络",0);
            }
        });
    }

    public interface matchFinishedListener{
        void done(List<UserSkill> bestUsers);
        void fail(String error);
    }
    //匹配
    private void matchFreinds(List<UserSkill> list,matchFinishedListener listener){

        //从本地读取当前用户的技能
        final String myWeakSkills[]=skillModel.readWeakSkills(AppApplication.getCurrentUser().getUsername());
        final String myStrongSkills[]=skillModel.readStrongSkills(AppApplication.getCurrentUser().getUsername());

        if(myWeakSkills==null||myWeakSkills.length==0){
            listener.fail("请到个人设置中设置想学习的技能");
            return;
        }

        /**
         * 首先把所有用户的匹配权值全部算出来
         */
        for(UserSkill u:list){
            List<String> strongs=u.getStrongSkills();
            List<String> weaks=u.getWeakSkills();

            //将我想学的技能和这个用户会的技能进行查找比较
            if(strongs!=null&&myWeakSkills!=null){
                for(String myWeak:myWeakSkills){
                    for(String strong:strongs){
                        if (myWeak.equals(strong)){
                            u.weight+=MATCH_WANT;     //匹配权值增加
                        }
                    }
                }
            }

            //将我会的技能和这个用户想学的技能进行查找比较
            if(weaks!=null&&myStrongSkills!=null) {
                for (String myStrong : myStrongSkills) {

                    for (String weak : weaks) {
                        if (myStrong.equals(weak)) {
                            u.weight += MATCH_CAN;      //匹配权值增加
                        }
                    }
                }
            }
        }
        //找到所有非0的用户
        List<UserSkill> bestUsers=new ArrayList<>();

        //对list进行排序
        Collections.sort(list);

        //把非0用户添加进去
        for (UserSkill u:list){
            if(u.weight>0){
                u.weight=u.weight*100/((MATCH_WANT+MATCH_CAN)*3);  //转换成为百分比
                bestUsers.add(u);
            }else{
                break;
            }
        }

        listener.done(bestUsers);
    }



//    //匹配用户
//
//    /**
//     * 这是原来的匹配方法，不使用了
//     * @param list
//     */
//    private void matchFreinds_old(List<UserSkill> list){
//        //从本地读取当前用户的技能
//        final String weakSkills[]=skillModel.readWeakSkills(AppApplication.getCurrentUser().getUsername());
//        final String strongSkills[]=skillModel.readStrongSkills(AppApplication.getCurrentUser().getUsername());
//
//        for(UserSkill u:list){
//            List<String> strongs=u.getStrongSkills();
//            List<String> weaks=u.getWeakSkills();
//            //将我想学的和这个用户会的进行查找
//            for(String w:weakSkills){
//                for(String strong:strongs){
//                    if (w.equals(strong)){
//                        int cnt=ordersDao.isSkillMatched(u.getUser().getUsername(),w);
//                        if(cnt==0){
//                            /** w匹配且w未被匹配过
//                             *  开始查看那个用户想学的和我会的也匹配
//                             */
//                            for(String s:strongSkills){
//                                for(String weak:weaks){
//                                    if(s.equals(weak)){
//                                        /**匹配成功
//                                         *
//                                         * 这里不管匹配的那个对象想学的是否和我曾经匹配过，
//                                         * 只要我想学的和他会的没匹配到过就行
//                                         */
//
//                                        //将对方匹配的技能写入本地，不关心我用什么技能和他匹配的
//                                        ordersDao.insertMatchedSkill(u.getUser().getUsername(),w);
//                                        ToastUtil.ToastShow(skillInterface.getThisActivity(),"匹配成功:  "+w+"--"+s,1);
//
//                                        //停止匹配动画
//                                        skillInterface.stopMatchAnimation();
//
//                                        BmobIMUserInfo info = new BmobIMUserInfo(u.getUser().getObjectId(), u.getUser().getUsername(), u.getUser().getHeadFileUrl());
//
//                                        BmobIMConversation conversationEntrance = BmobIM.getInstance().startPrivateConversation(info, null);
//                                        Bundle bundle = new Bundle();
//                                        bundle.putSerializable("c", conversationEntrance);
//
//
//                                        Intent intent = new Intent();
//                                        intent.setClass(skillInterface.getThisActivity(), ChatActivity.class);
//                                        if (bundle != null)
//                                            intent.putExtra(skillInterface.getThisActivity().getPackageName(), bundle);
//                                        skillInterface.getThisActivity().startActivity(intent);
//
//                                        return;
//                                    }
//                                }
//                            }
//                        }else{
//                            ToastUtil.ToastShow(skillInterface.getThisActivity(),
//                                    "已经和"+u.getUser().getUsername()+"会的 "+strong+" 匹配过"+cnt+"次",1);
//                        }
//                    }
//                }
//            }
//        }
//        //所有用户都没有匹配到,匹配失败
//        skillInterface.stopMatchAnimation();
//        ToastUtil.ToastShow(skillInterface.getThisActivity(),"已经把所有用户匹配完了，但还没找到合适你的",1);
//    }
}
